//后退按钮
$(document).on('click', ".backBtn", function () {
    history.go(-1);
})

//网站域名
var su = $("#staticUrl").val();

//工具提示
$('[data-toggle="tooltip"]').tooltip();
$('[data-toggle="popover"]').popover();

//时间插件
laydate.render({
    elem: '.datetime'
    , type: 'datetime'
});

//选择单个
$("[data-type='checkbox']").click(function () {
    var myCount = 0;
    $.each($("[data-type='checkbox']"), function (i) {
        if ($(this).prop("checked") == true) {
            myCount++;
        }
    })
    var count = $("[data-type='checkbox']").length
    if (myCount == count) {
        $("[data-type='checkall']").prop("checked", true);
    } else {
        $("[data-type='checkall']").prop("checked", false);
    }
})
//全选
$("[data-type='checkall']").click(function () {
    if ($(this).prop("checked")) {
        $("[data-type='checkbox']").prop("checked", true);
    } else {
        $("[data-type='checkbox']").prop("checked", false);
    }
})

//修改用户信息
$(".editUserData").on("click", function () {
    formWindow("修改用户信息", "/fyl-crm-page-v1/page/edit-user-data.html", function (index) {
        layer.close(index);
    })
});
//修改用户密码
$(".editUserPwd").on("click", function () {
    formWindow("修改用户密码", "/fyl-crm-page-v1/page/edit-user-pwd.html", function (index) {
        layer.close(index);
    })
});

//消息提示
function msgAlert(text) {
    layer.msg(text, {time: 1000});
}

//消息提示
function msgAlertCallback(text, callback) {
    layer.msg(text, {time: 1000}, function () {
        callback();
    });
}

//正确消息提示
function yesAlert(text) {
    layer.alert(text, {icon: 1, title: '提示'});
}

//正确消息提示
function yesAlertCallback(text, callback) {
    layer.alert(text, {icon: 1, title: '提示'}, function (index) {
        callback();
        layer.close(index);
    });
}

//错误消息提示
function noAlert(text) {
    layer.alert(text, {icon: 2, title: '提示'});
}

//异步ajax提交
function ajaxJson(url, data, callBack) {
    $.ajax({
        url: url,
        type: "post",
        data: data,
        dataType: "json",
        success: callBack,
        error: function () {
            // noAlert("操作失败")
            console.log("操作失败")
        }
    });
}

//异步ajax提交
function ajaxText(url, data, callBack) {
    $.ajax({
        url: url,
        type: "get",
        data: data,
        dataType: "text",
        success: callBack,
        error: function () {
            noAlert("操作失败")
        }
    });
}

//清空搜索条件
$(".clearSearch").on("click", function () {
    console.log(1)
    $('.input-clear').val('');
    $('.select-clear').each(function (index, em) {
        $(em)[0].selectedIndex = 0;
    })
    $('.select-clear').change();
})

//开始查询
$(".startSearch").on("click", function () {
    console.log(2)
    $("#page").val(0)
    $("#form").submit();
})

//删除提示
function confirmAlert(text, callBack) {
    layer.confirm(text, {icon: 3, title: '提示'}, function (index) {
        layer.close(index, function () {
            callBack();
        });
    });
}

//表单窗口
function lgFormWindow(title, url, callBack) {
    //指定宽高
    layer.open({
        type: 2,
        // shade: 0,
        // anim: 4, //默认动画风格
        btnAlign: 'l',
        title: title,
        minStack: false,
        // move: false,//禁止移动
        area: ["70%", '90%'],
        btn: ['提交', '关闭'],
        yes: function (index, layero) {
            callBack(index, layero);
        },
        content: url,
    });
}

function formWindow(title, url, callBack) {
    //指定宽高
    layer.open({
        type: 2,
        // shade: 0,
        // anim: 4, //默认动画风格
        btnAlign: 'l',
        title: title,
        minStack: false,
        // move: false,//禁止移动
        area: ["60%", '90%'],
        btn: ['提交', '关闭'],
        yes: function (index, layero) {
            callBack(index, layero);
        },
        content: url,
    });
}

//页面窗口
function pageWindow(title, url) {
    //指定宽高
    layer.open({
        type: 2,
        // shade: 0,
        // anim: 4, //默认动画风格
        btnAlign: 'l',
        title: title,
        minStack: false,
        move: false,//禁止移动
        area: ["100%", '100%'],
        content: url,
    });
}

function lgPageWindow(title, url) {
    //指定宽高
    layer.open({
        type: 2,
        // shade: 0,
        // anim: 4, //默认动画风格
        btnAlign: 'l',
        title: title,
        minStack: false,
        // move: false,//禁止移动
        area: ["70%", '90%'],
        content: url,
    });
}

//获取子页面form元素
function iframeForm(layero) {
    return $(layero).find("iframe").contents().find("form");
}

//设置子页面错误消息
function iframeFormError(form, field) {
    form.find("#" + field.name).text(field.value);
    form.find("#" + field.name).show();
}

//分页搜索
function pageSearch(page) {
    $("#page").val(page - 1)
    $("#form").submit();
}

//上传图片
function uploadImg(btnSelector, dir, cb) {
    var accept = "image/jpeg,image/png,image/gif";
    uploadFun(btnSelector, dir, accept, cb);
}

//上传文件
function uploadFile(btnSelector, dir, cb) {
    var accept = "";
    uploadFun(btnSelector, dir, accept, cb);
}

//上传文件功能
function uploadFun(btnSelector, dir, accept, cb) {
    //添加上传控件
    var file = '<input type="file" name="file" class="hidden" dir="' + dir + '" accept="' + accept + '">';
    $(btnSelector).after(file);
    //触发上传控件
    $(btnSelector).click(function () {
        $(this).next().trigger("click");
    })
    //上传控件绑定切换事件
    $(btnSelector).next().change(function () {
        var formData = new FormData();
        formData.append("file", $(btnSelector).next()[0].files[0]);
        formData.append("dir", $(btnSelector).next().attr("dir"));
        upload("/upload/file", formData, function (re) {
            if (re.status) {
                cb(re.data);
            } else {
                noAlert(re.message)
            }
        })
        $(btnSelector).next().val("");
    })
}

//异步上传文件
function upload(url, data, cb) {
    $.ajax({
        url: url,
        type: "post",
        data: data,
        dataType: "json",
        cache: false,
        processData: false,
        contentType: false,
        success: cb,
        error: function () {
            noAlert("操作失败");
        }
    });
}